package com.scuec.groupfive.service.impl;

import com.scuec.groupfive.entity.FavoriteFolder;
import com.scuec.groupfive.entity.Questions;
import com.scuec.groupfive.mapper.FavoriteFolderMapper_z;
import com.scuec.groupfive.mapper.QuestionMapper_z;
import com.scuec.groupfive.service.FavoriteFolderService_z;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

@Service
public class FavoriteFolderServiceImpl_z implements FavoriteFolderService_z {
    private static final Logger logger = LoggerFactory.getLogger(FavoriteFolderServiceImpl_z.class);

    @Resource
    private FavoriteFolderMapper_z favoriteFolderMapper_z;

    @Resource
    private QuestionMapper_z questionMapperZ;

    @Override
    public List<FavoriteFolder> getFoldersByUserId(Long userId) {
        try {
            logger.info("Getting folders for user: {}", userId);
            List<FavoriteFolder> folders = favoriteFolderMapper_z.findByUserId(userId);
            logger.info("Found {} folders for user {}", folders.size(), userId);
            return folders;
        } catch (Exception e) {
            logger.error("Error getting folders for user {}: {}", userId, e.getMessage(), e);
            throw new RuntimeException("获取收藏夹列表失败: " + e.getMessage());
        }
    }

    @Override
    public List<Questions> getQuestionsByContent(Long userId, Long folderId, String examType,
                                                 String questionType, String subjectId, Integer offset, Integer pageSize) {
        logger.info("Fetching questions for user {} with folderId: {}, examType: {}, questionType: {}, subjectId: {}, offset: {}, pageSize: {}",
                userId, folderId, examType, questionType, subjectId, offset, pageSize);
        List<Questions> questions = favoriteFolderMapper_z.selectQuestionsByContent(userId, folderId, examType,
                questionType, subjectId, offset, pageSize);
        logger.info("Found {} questions", questions.size());
        return questions;
    }

    @Override
    @Transactional
    public FavoriteFolder createFolder(FavoriteFolder folder) {
        favoriteFolderMapper_z.insert(folder);
        return folder;
    }

    @Override
    @Transactional
    public FavoriteFolder updateFolder(FavoriteFolder folder) {
        favoriteFolderMapper_z.update(folder);
        return folder;
    }

    @Override
    @Transactional
    public void deleteFolder(Long id) {
        favoriteFolderMapper_z.delete(id);
    }

    @Override
    @Transactional
    public void moveQuestion(Long userId, Long questionId, Long folderId) {
        // 检查题目是否存在
        Questions question = questionMapperZ.selectById(questionId);
        if (question == null) {
            throw new RuntimeException("题目不存在，无法移动到收藏夹");
        }

        // 检查收藏夹是否存在
        List<FavoriteFolder> folders = favoriteFolderMapper_z.getFoldersByUserId(userId);
        boolean folderExists = folders.stream()
                .anyMatch(f -> f.getId().equals(folderId));
        if (!folderExists) {
            throw new RuntimeException("收藏夹不存在");
        }

        favoriteFolderMapper_z.moveQuestion(userId, questionId, folderId);
    }

    @Override
    public Integer countQuestions(Long userId, Long folderId, String examType,
                                  String questionType, String subjectId) {
        return favoriteFolderMapper_z.countQuestions(userId, folderId, examType,
                questionType, subjectId);
    }

    @Override
    public void deleteQuestionFromFolder(Long userId, Long questionId) {
        favoriteFolderMapper_z.deleteQuestionFromFolder(userId, questionId);
    }

    @Override
    public List<Questions> getFavoriteQuestions(Long userId, Long folderId, String examType, String questionType, String subjectId, Integer page, Integer pageSize) {
        int offset = (page - 1) * pageSize;
        return favoriteFolderMapper_z.selectQuestionsByContent(userId, folderId, examType, questionType, subjectId, offset, pageSize);
    }

    @Override
    public Integer countFavoriteQuestions(Long userId, Long folderId, String examType, String questionType, String subjectId) {
        return favoriteFolderMapper_z.countQuestions(userId, folderId, examType, questionType, subjectId);
    }

    @Override
    @Transactional
    public void addQuestionToFolder(Long userId, Long questionId, Long folderId) {
        // 检查题目是否存在
        Questions question = questionMapperZ.selectById(questionId);
        if (question == null) {
            throw new RuntimeException("题目不存在，无法添加到收藏夹");
        }

        // 检查收藏夹是否存在
        List<FavoriteFolder> folders = favoriteFolderMapper_z.getFoldersByUserId(userId);
        boolean folderExists = folders.stream()
                .anyMatch(f -> f.getId().equals(folderId));
        if (!folderExists) {
            throw new RuntimeException("收藏夹不存在");
        }

        favoriteFolderMapper_z.addQuestionToFolder(userId, questionId, folderId);
    }
}